﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ComicsEditor
{
    public delegate void ActivePointMoveHandler(ActivePoint activePoint, Point oldCenter, Point newCenter);

    public class ActivePoint : PageObject
    {
        public Point Center;

        public event ActivePointMoveHandler ActivePointMove;
        public Rectangle Bounds;

        public ActivePoint(Page Page, Point Center) : base(Page)
        {
            this.Center = Center;
            Bounds = new Rectangle(0, 0, Page.Width, Page.Height);
        }

        public override void Draw(Graphics Graphics)
        {
            RectangleF rectF = new RectangleF(Page.Transform(Center), new SizeF(6f, 6f));
            Rectangle rect = new Rectangle((int)rectF.X - 3, (int)rectF.Y - 3, (int)rectF.Width, (int)rectF.Height);
            Graphics.FillRectangle(new SolidBrush(Color.White), rect);
            Graphics.DrawRectangle(new Pen(Color.Black), rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
        }

        public override bool Activate(Point point, out Point offset)
        {
            offset = new Point(Center.X - point.X, Center.Y - point.Y);
            Size hotspot = Page.Transform(new SizeF(5, 5));
            if (Math.Abs(offset.X) <= hotspot.Width && Math.Abs(offset.Y) <= hotspot.Height)
                return true;
            return false;
        }

        public override void Drag(Point newPoint, Point offset, Point delta, Point deltaMove)
        {
            Point oldCenter = Center;
            Center.X = newPoint.X + offset.X;
            Center.Y = newPoint.Y + offset.Y;
            if (Center.X < Bounds.Left)
                Center.X = Bounds.Left;
            if (Center.Y < Bounds.Top)
                Center.Y = Bounds.Top;
            if (Center.X >= Bounds.Right)
                Center.X = Bounds.Right - 1;
            if (Center.Y >= Bounds.Bottom)
                Center.Y = Bounds.Bottom - 1;
            if (oldCenter.X != Center.X || oldCenter.Y != Center.Y)
            {
                if (ActivePointMove != null)
                    ActivePointMove(this, oldCenter, Center);
                Page.Changed = true;
            }
        }
    }
}
